<?php

namespace app\logic\admin;

class L_goods
{
    /**
     * 创建数据
     */
    public function store($paramObj=""){
        $dataObj = paramsObj();
        $dataObj->request = paramsObj();
        $dataObj->data = paramsObj();
        $dataObj->data->use_trans = false;
        $dataObj->result = new \ArrayObject();

        $final_arr = logInit();
        if ($final_arr["error_no"] == 0) {
            $mixed = parseToArr($paramObj, "mixed", 0);
            $params = parseToArr($paramObj, "params", array());
            $messages = parseToArr($paramObj, "messages", array());
            $rules = array(
                'name' => 'require',
                '_goodsNo' => 'require',
                '_marketPrice' => 'require',
                '_sellPrice' => 'require',
                '_costPrice' => 'require',
                '_storeNums' => 'require',
                '_weight' => 'require',
                '_commendGoods' => '',
                'status' => 'require',
                'isVirtual' => 'require',
                'modelId' => '',
                'thumb' => '',
                'keywords' => '',
                'description' => '',
                'searchWords' => '',
                'sort' => '',
                '_specMap' => '',
                'sellerId' => '',
                'categoryId' => '',
                'isShare' => '',
                'content' => '',
                'unit' => 'requireIf:isVirtual,0',
                '_virtualConfig' => 'requireIf:isVirtual,1',
                '_virtualType' => 'requireIf:isVirtual,1',
            );
            $paramsObj = paramsObj();
            $paramsObj->params = $params;
            $paramsObj->mixed = $mixed;
            $paramsObj->rules = $rules;
            $paramsObj->messages = $messages;
            $checkResult = app("verifyParam")::validatorRequest($paramsObj);
            if ($checkResult["error_no"] == 0) {
                $dataObj->request = $checkResult["result"];
            } else {
                $final_arr = logCallErrorMsg($final_arr, $checkResult);
            }
        }
        if ($final_arr["error_no"] == 0) {
            //数据过滤
            $messages = parseToArr($paramObj, "messages", array());
            foreach($dataObj->request->_goodsNo as $key=>$val){
                if ($final_arr["error_no"] == 0) {
                    $data=array(
                        "_goodsNo"=>$val,
                        "_marketPrice"=>$dataObj->request->_marketPrice[$key],
                        "_sellPrice"=>$dataObj->request->_sellPrice[$key],
                        "_costPrice"=>$dataObj->request->_costPrice[$key],
                        "_storeNums"=>$dataObj->request->_storeNums[$key],
                        "_weight"=>$dataObj->request->_weight[$key],
                        "_virtualConfig"=>$dataObj->request->_virtualConfig[$key],
                        "_virtualType"=>$dataObj->request->_virtualType[$key],
                    );
                    $rules = array(
                        '_goodsNo' => 'require',
                        '_storeNums' => 'requireIf:isVirtual,0',
                        '_marketPrice' => 'require',
                        '_sellPrice' => 'require',
                        '_costPrice' => 'requireIf:isVirtual,0',
                        '_weight' => 'requireIf:isVirtual,0',
                        '_virtualConfig' => 'requireIf:isVirtual,1',
                        '_virtualType' => 'requireIf:isVirtual,1',
                    );
                    $paramsObj = paramsObj();
                    $paramsObj->params = $data;
                    $paramsObj->rules = $rules;
                    $paramsObj->messages = $messages;
                    $checkResult = app("verifyParam")::validatorRequest($paramsObj);
                    if ($checkResult["error_no"] == 0) {

                    } else {
                        $final_arr = logCallErrorMsg($final_arr, $checkResult);
                    }
                }
                if ($final_arr["error_no"]) {
                    break;
                }
            }
        }
        if ($final_arr["error_no"] == 0) {
            Db()->startTrans();
            $dataObj->data->use_trans = true;
        }
        if($final_arr["error_no"]==0){//创建商品
            //组装-规格-数据
            $specMap=array();
            if($dataObj->request->_specMap){
                $newData=array();
                $finalData=array();
                foreach($dataObj->request->_specMap as $val){
                    foreach($val as $zval){
                        $newData[$zval["id"]][]=$val["value"];
                        if(isset($finalData[$zval["id"]])){
                            $finalData[$zval["id"]]["value"]=$newData;
                        }else{
                            $finalData[$zval["id"]]=$zval;
                        }
                    }
                }
                $specMap=$finalData;
            }
            $paramsObj=paramsObj();
            $paramsObj->params=array(
                "goodsNo"=>$dataObj->request->_goodsNo[0],
                'name' => $dataObj->request->name,
                'categoryId' => $dataObj->request->categoryId,
                'sellPrice' => $dataObj->request->_sellPrice[0],
                'marketPrice' => $dataObj->request->_marketPrice[0],
                'costPrice' => $dataObj->request->_costPrice[0],
                'storeNums' => array_sum($dataObj->request->_storeNums),
                'weight' => $dataObj->request->_weight[0],
                'status' =>$dataObj->request->status,
                'isVirtual' => $dataObj->request->isVirtual,
                'modelId' => $dataObj->request->modelId,
                'thumb' => $dataObj->request->thumb,
                'keywords' => $dataObj->request->keywords,
                'description' => $dataObj->request->description,
                'searchWords' => $dataObj->request->searchWords,
                'sort' => $dataObj->request->sort,
                'specMap' =>$specMap ,
                'isShare' => $dataObj->request->isShare,
                'content' => $dataObj->request->content,
                'unit' => $dataObj->request->unit,
                'virtualConfig' => $dataObj->request->isVirtual?$dataObj->request->_virtualConfig[0]:"",
                'virtualType' => $dataObj->request->isVirtual?$dataObj->request->_virtualType[0]:"",
            );
            $checkResult=app("S_Admin_goods")->store($paramsObj);
            if($checkResult["error_no"]==0){

            }else{
                $final_arr=logCallErrorMsg($final_arr,$checkResult);
            }
        }
        if($final_arr["error_no"]==0){
            //关联推荐
            if($dataObj->request->_commendGoods){

            }
        }
//        if($final_arr["error_no"]==0){
//            $_goodsNos=$dataObj->request->_goodsNo;
//            foreach($_goodsNos as $key=>$val){
//                if($final_arr["error_no"]==0){
//                    //创建货品
//                    $paramsObj=paramsObj();
//                    $paramsObj->params=array(
//                        "goodsNo"=>$val,
//                        'name' => $dataObj->request->name,
//                        'sellPrice' => $dataObj->request->_sellPrice[$key],
//                        'marketPrice' => $dataObj->request->_marketPrice[$key],
//                        'costPrice' => $dataObj->request->_costPrice[$key],
//                        'storeNums' => $dataObj->request->_storeNums[$key],
//                        'weight' => $dataObj->request->_weight[$key],
//                        'status' =>$dataObj->request->status,
//                        'isVirtual' => $dataObj->request->isVirtual,
//                        'modelId' => $dataObj->request->modelId,
//                        'thumb' => $dataObj->request->thumb,
//                        'keywords' => $dataObj->request->keywords,
//                        'description' => $dataObj->request->description,
//                        'searchWords' => $dataObj->request->searchWords,
//                        'sort' => $dataObj->request->sort,
//                        'specMap' => $dataObj->request->_specMap[$key],
//                        'isShare' => $dataObj->request->isShare,
//                        'content' => $dataObj->request->content,
//                        'unit' => $dataObj->request->unit,
//                        'virtualConfig' => $dataObj->request->_virtualConfig[$key],
//                    );
//                    dd($paramsObj);
//                    $checkResult=app("S_Admin_goods")->store($paramsObj);
//                    if($checkResult["error_no"]==0){
//
//                    }else{
//                        $final_arr=logCallErrorMsg($final_arr,$checkResult);
//                    }
//                }
//                if($final_arr["error_no"]){
//                    break;
//                }
//            }
//        }
        if ($dataObj->data->use_trans) {
            if ($final_arr["error_no"] == 0) {
                Db()->commitTrans();
            } else {
                Db()->rollbackTrans();
            }
        }
        return $final_arr;
    }

    /**
     * 更新数据
     */
    public function update($paramObj=""){
        $dataObj = paramsObj();
        $dataObj->request = paramsObj();
        $dataObj->data = paramsObj();
        $dataObj->data->use_trans = false;
        $dataObj->result = new \ArrayObject();

        $final_arr = logInit();
        if ($final_arr["error_no"] == 0) {
            $mixed = parseToArr($paramObj, "mixed", 0);
            $params = parseToArr($paramObj, "params", array());
            $messages = parseToArr($paramObj, "messages", array());
            $rules = array(
                'id' => 'require',
                'name' => 'require',
                '_goodsNo' => 'require',
                '_marketPrice' => 'require',
                '_sellPrice' => 'require',
                '_costPrice' => 'require',
                '_storeNums' => 'require',
                '_weight' => 'require',
                '_commendGoods' => '',
                'status' => 'require',
                'isVirtual' => 'require',
                'modelId' => '',
                'thumb' => '',
                'keywords' => '',
                'description' => '',
                'searchWords' => '',
                'sort' => '',
                '_specMap' => '',
                'sellerId' => '',
                'isShare' => '',
                'content' => '',
                'categoryId' => '',
                'unit' => 'requireIf:isVirtual,0',
                '_virtualConfig' => 'requireIf:isVirtual,1',
                '_virtualType' => 'requireIf:isVirtual,1',
            );
            $paramsObj = paramsObj();
            $paramsObj->params = $params;
            $paramsObj->mixed = $mixed;
            $paramsObj->rules = $rules;
            $paramsObj->messages = $messages;
            $checkResult = app("verifyParam")::validatorRequest($paramsObj);
            if ($checkResult["error_no"] == 0) {
                $dataObj->request = $checkResult["result"];
            } else {
                $final_arr = logCallErrorMsg($final_arr, $checkResult);
            }
        }
        if ($final_arr["error_no"] == 0) {
            //数据过滤
            $messages = parseToArr($paramObj, "messages", array());
            foreach($dataObj->request->_goodsNo as $key=>$val){
                if ($final_arr["error_no"] == 0) {
                    $data=array(
                        "_goodsNo"=>$val,
                        "_marketPrice"=>$dataObj->request->_marketPrice[$key],
                        "_sellPrice"=>$dataObj->request->_sellPrice[$key],
                        "_costPrice"=>$dataObj->request->_costPrice[$key],
                        "_storeNums"=>$dataObj->request->_storeNums[$key],
                        "_weight"=>$dataObj->request->_weight[$key],
                        "_virtualConfig"=>$dataObj->request->_virtualConfig[$key],
                        "_virtualType"=>$dataObj->request->_virtualType[$key],
                    );
                    $rules = array(
                        '_goodsNo' => 'require',
                        '_storeNums' => 'requireIf:isVirtual,0',
                        '_marketPrice' => 'require',
                        '_sellPrice' => 'require',
                        '_costPrice' => 'requireIf:isVirtual,0',
                        '_weight' => 'requireIf:isVirtual,0',
                        '_virtualConfig' => 'requireIf:isVirtual,1',
                        '_virtualType' => 'requireIf:isVirtual,1',
                    );
                    $paramsObj = paramsObj();
                    $paramsObj->params = $data;
                    $paramsObj->rules = $rules;
                    $paramsObj->messages = $messages;
                    $checkResult = app("verifyParam")::validatorRequest($paramsObj);
                    if ($checkResult["error_no"] == 0) {

                    } else {
                        $final_arr = logCallErrorMsg($final_arr, $checkResult);
                    }
                }
                if ($final_arr["error_no"]) {
                    break;
                }
            }
        }
        if ($final_arr["error_no"] == 0) {
            Db()->startTrans();
            $dataObj->data->use_trans = true;
        }
        if($final_arr["error_no"]==0){//创建商品
            //组装-规格-数据
            $specMap=array();
            if($dataObj->request->_specMap){
                $newData=array();
                $finalData=array();
                foreach($dataObj->request->_specMap as $val){
                    foreach($val as $zval){
                        $newData[$zval["id"]][]=$val["value"];
                        if(isset($finalData[$zval["id"]])){
                            $finalData[$zval["id"]]["value"]=$newData;
                        }else{
                            $finalData[$zval["id"]]=$zval;
                        }
                    }
                }
                $specMap=$finalData;
            }
            $paramsObj=paramsObj();
            $paramsObj->params=array(
                "goodsNo"=>$dataObj->request->_goodsNo[0],
                'id' => $dataObj->request->id,
                'name' => $dataObj->request->name,
                'categoryId' => $dataObj->request->categoryId,
                'sellPrice' => $dataObj->request->_sellPrice[0],
                'marketPrice' => $dataObj->request->_marketPrice[0],
                'costPrice' => $dataObj->request->_costPrice[0],
                'storeNums' => array_sum($dataObj->request->_storeNums),
                'weight' => $dataObj->request->_weight[0],
                'status' =>$dataObj->request->status,
                'isVirtual' => $dataObj->request->isVirtual,
                'modelId' => $dataObj->request->modelId,
                'thumb' => $dataObj->request->thumb,
                'keywords' => $dataObj->request->keywords,
                'description' => $dataObj->request->description,
                'searchWords' => $dataObj->request->searchWords,
                'sort' => $dataObj->request->sort,
                'specMap' =>$specMap ,
                'isShare' => $dataObj->request->isShare,
                'content' => $dataObj->request->content,
                'unit' => $dataObj->request->unit,
                'virtualConfig' => $dataObj->request->isVirtual?$dataObj->request->_virtualConfig[0]:"",
                'virtualType' => $dataObj->request->isVirtual?$dataObj->request->_virtualType[0]:"",
            );
            $checkResult=app("S_Admin_goods")->update($paramsObj);
            if($checkResult["error_no"]==0){

            }else{
                $final_arr=logCallErrorMsg($final_arr,$checkResult);
            }
        }
        if ($dataObj->data->use_trans) {
            if ($final_arr["error_no"] == 0) {
                Db()->commitTrans();
            } else {
                Db()->rollbackTrans();
            }
        }
        return $final_arr;
    }

    public function __call($name, $arguments)
    {
        return recordLogMsgLevelTwo(app("errorLevel")::E_WARNING, app("errorCode")::UNDEFINED_METHOD, lang("sys.noMethodDefined"), $name, $arguments);
    }
}